import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;

public class TimeForm extends HttpServlet {
	private final String ACTION_PATH = "/time/local";
	private final String QUERY_LOCALE   = "locale";
	private final String QUERY_TIMEZONE = "timezone";
    
    Map<String, Locale> localeMap = new TreeMap<String, Locale>();
    Map<String, String> timezoneMap = new TreeMap<String, String>();

    @Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) 
		throws IOException, ServletException {

		resp.setContentType("text/html");
		PrintWriter writer = resp.getWriter();

		writer.println("<html>");
		writer.println("<head><title>Choose Locale and Time Zone</title></head>");
		writer.println("<body>");
		writer.println("<h1>Choose locale and time zone</h1>");
		writer.println("<form method=\"POST\" action=\"" + ACTION_PATH + "\">");
		writer.println("Locale <select name=\"" + QUERY_LOCALE + "\">");
		//
		// DONE:
		//
        for (Locale locale : Locale.getAvailableLocales()) {
            if (!localeMap.containsKey(locale.getDisplayCountry())) {
                localeMap.put(locale.getDisplayCountry(), locale);
            }
        }
        
        for (String country : localeMap.keySet()) {
            writer.println("<option value=\"" + localeMap.get(country) + "\">" 
                + country + "</option>");
        }
        
		writer.println("</select><br />");
		writer.println("Time Zone <select name=\"" + QUERY_TIMEZONE + "\" />");
		//
		// DONE:
		//
        timezoneMap.put("", "");
        for (String timezone : TimeZone.getAvailableIDs()) {
            if (!timezoneMap.containsKey(timezone)) {
                timezoneMap.put(timezone, timezone);
            }
        }
        
        for (String timezone: timezoneMap.keySet()) {
            writer.println("<option value=\"" + timezone + "\">" 
                + timezone + "</option>");
        }
        
		writer.println("</select><br />");
		writer.println("<input type=\"submit\" value=\"Submit\" />");
		writer.println("<input type=\"reset\" value=\"Reset\" />");
		writer.println("</form>");
		writer.println("</body>");
		writer.println("</html>");
	}

    @Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws IOException, ServletException {
		//
		// DONE:
		//
        TimeZone sysTimezone = TimeZone.getDefault();
        
        TimeZone timezone = TimeZone.getTimeZone(req.getParameter(QUERY_TIMEZONE));
        TimeZone.setDefault(timezone);
        
        Locale locale = new Locale(req.getParameter(QUERY_LOCALE));
        Calendar calendar = Calendar.getInstance(timezone, locale);
        
        resp.setContentType("text/html");
		PrintWriter writer = resp.getWriter();
        
        writer.println("<html>");
		writer.println("<head><title>Current Time</title></head>");
		writer.println("<body>");
		writer.println("<h1>" + timezone.getDisplayName() + " " 
            + calendar.getTime() + "</h1>");
        writer.println("</body>");
        writer.println("</html>");
        
        TimeZone.setDefault(sysTimezone);
	}
}
